var express = require('express');
var router = express.Router();
const formidable = require('formidable');
const {resolve} = require('path');

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', {
    title: 'Express'
  });
});

router.post('/server', (req, res, next) => {
  //创建 form 对象
  const form = formidable({
    multiples: true,
    uploadDir: resolve(__dirname, '../public/uploads'),
    keepExtensions: true
  });

  //           错误对象  普通表单数据   文件上传数据
  form.parse(req, (err, fields,       files) => {
    if (err) {
      next(err);
      return;
    }
    //拼接图片的访问 url
    let imageURL = '/uploads/' + files.profile.newFilename;

    res.send(`
      <h2>我的头像</h2>
      <img src="${imageURL}" />
    `)
  });
});

module.exports = router;